//
//  DisconveryActicleCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/9/2.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "DisconveryActicleCell.h"

@implementation DisconveryActicleCell

#pragma mark - lifecycle methods

- (void)setupUI{
    
    [self addSubview:self.titleLab];
    [self addSubview:self.bottomLab];
    [self addSubview:self.image1];
    [self addSubview:self.image2];
    [self addSubview:self.image3];
    [_image1 addSubview:self.player];
    [_image1 addSubview:self.progressLab];
    
}

- (void)setupLayout
{
    {
        _titleLab.mj_x = 15;
        _titleLab.mj_y = 15;
        _titleLab.height = 52;
    }

    {
        _image1.height = _image2.height = _image3.height = 78;
    }

    switch (self.type) {
        case DisconveryActicleCellType_None:
            {
                self.height = 108;
                _titleLab.width = KScreenWidth - 30;
                _image1.hidden = _image2.hidden = _image3.hidden = YES;
                _bottomLab.width = KScreenWidth - 30;
                
                _player.hidden = _progressLab.hidden = YES;
            }
            break;
            
        case DisconveryActicleCellType_Single:
            {
                self.height = 108;
                
                _titleLab.width = KScreenWidth - 170;
                
                _image1.hidden = NO;
                _image2.hidden = _image3.hidden = YES;
                
                _bottomLab.width = KScreenWidth - 170;
                
                _image1.mj_x = KScreenWidth - 117 - 15;
                _image1.width = 117;
                _image1.mj_y = 15;
                
                _player.hidden = _progressLab.hidden = YES;

            }
            break;
        case DisconveryActicleCellType_More:
            {
                self.height = 195;
                _titleLab.width = KScreenWidth - 30;
                _image1.hidden = _image2.hidden = _image3.hidden = NO;

                CGFloat gap  =( KScreenWidth  - 30 - 4*3)/3.0;
                _image1.width = _image2.width = _image3.width = gap;
                _image1.mj_x = 15;
                _image2.mj_x = _image1.right + 4;
                _image3.mj_x = _image2.right + 4;
                
                _image1.mj_y = _image2.mj_y = _image3.mj_y = _titleLab.bottom + 8;
                
                _bottomLab.width = KScreenWidth - 30;

                _player.hidden = _progressLab.hidden = YES;

            }
            break;
            
            break;
        case DisconveryActicleCellType_Video:
        {
            self.height = 281;
            {
                _titleLab.width = KScreenWidth - 30;
                _bottomLab.width = KScreenWidth - 30;
            }
            
            {
                _image1.hidden = NO;
                _image2.hidden = _image3.hidden = YES;
                
                _image1.mj_x = 15;
                _image1.width = KScreenWidth - 30;
                _image1.height = 164;
                _image1.mj_y = _titleLab.bottom + 8;
            }
            
            {
                _player.hidden = _progressLab.hidden = NO;
                _player.size = CGSizeMake(40, 40);
                _player.mj_x = (_image1.width - _player.width)/2.0;
                _player.mj_y = (_image1.height - _player.height)/2.0;

                _progressLab.size = CGSizeMake(62, 18);
                _progressLab.mj_x = _image1.width - _progressLab.width - 10;
                _progressLab.mj_y = _image1.height - _progressLab.height - 10;
            }
        }
            break;

            
        default:
            
            break;
    }
    
    
    {
        _bottomLab.mj_x = 15;
        _bottomLab.height = 17;
        _bottomLab.mj_y = self.height - _bottomLab.height - 15;
    }
}


#pragma mark - setter methods

- (void)setItem:(FoundActicleListItem *)item{
    
    //设置Type
    
    self.type = [item.myType integerValue];
    
    //
    _titleLab.text = item.articleTitle;
    
    NSString *time = [NSString getDateDisplayString:item.createTime];
    NSString *text = [NSString stringWithFormat:@"%@  %@   %@已读", item.source, item.createTime, item.readNum];
    _bottomLab.text = text;
    _bottomLab.font = mFont(12);
    _bottomLab.textColor = UIColorFromRGB(0x8C8C8C);
    
    @weakify(self);
    [item.materials enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        
        if (idx == 0) {
            NSString *image = self.type == DisconveryActicleCellType_Video? @"placeholder_large" :@"placeholder_middle";
            [self.image1  sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:[UIImage imageNamed:image]];
        }
        if (idx == 1) {
            [self.image2  sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:[UIImage imageNamed:@"placeholder_middle"]];
        }
        if (idx == 2) {
            [self.image3  sd_setImageWithURL:[NSURL URLWithString:obj] placeholderImage:[UIImage imageNamed:@"placeholder_middle"]];
        }
    }];
    
    [self setupLayout];
    
}

#pragma mark - getter methods

- (UILabel *)titleLab{
    
    if (!_titleLab) {
        _titleLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x343434) font:mFont(17) textAliment:0];
        _titleLab.numberOfLines = 0;
    }
    return _titleLab;
}


- (UILabel *)bottomLab{
    
    if (!_bottomLab) {
        _bottomLab = [UILabel new];
    }
    return _bottomLab;
}


- (UIImageView *)image1{
    
    if (!_image1) {
        _image1 = [UIImageView new];
        _image1.hidden = YES;
    }
    return _image1;
}

- (UIImageView *)image2{
    
    if (!_image2) {
        _image2 = [UIImageView new];
        _image2.hidden = YES;

    }
    return _image2;
}

- (UIImageView *)image3{
    
    if (!_image3) {
        _image3 = [UIImageView new];
        _image3.hidden = YES;
        _image3.backgroundColor = KRedColor;

    }
    return _image3;
}

- (UIImageView *)player{
    
    if (!_player) {
        _player = [UIImageView new];
        _player.image = [UIImage imageNamed:@"discovery_player"];
        _player.hidden = YES;
        
        @weakify(self);
        _player.tapBlock = ^{
            @strongify(self);
            if (self.dealPlayerBlock) {
                self.dealPlayerBlock();
            }
        };
    }
    return _player;
}


- (UILabel *)progressLab{
    
    if (!_progressLab) {
        _progressLab = [[UILabel alloc] init];
        _progressLab.backgroundColor = KBlackColor;
        NSMutableAttributedString *atttibutedStr = [@" 03:43" createAttributingByIcon:@"discovery_play_ progress" downIndexImage:nil textColor:UIColorFromRGB(0xE8E8E8) font:mFont(12)];
        _progressLab.attributedText = atttibutedStr;
        [self addShadowAndCircleCorner:_progressLab.layer corner:9];
    }
    return _progressLab;
}


@end
